package cn.ncist.health_service_provider.service.impl;

import cn.ncist.health_interface.service.MemberService;
import cn.ncist.health_service_provider.dao.MemberMapper;
import cn.ncist.health_service_provider.dao.OrderMapper;
import cn.ncist.health_service_provider.dao.SetMealMapper;
import cn.ncist.pojo.Member;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Transactional
@DubboService(interfaceClass = MemberService.class, timeout = 50000)
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private SetMealMapper setMealMapper;

    @Override
    public List<Integer> findMemberCountByMonths(List<String> months) {
        if (months.size() < 0 || months == null){
            throw new RuntimeException("传参错误");
        }
        List<Integer> memberCount = new ArrayList<>();
        for (String month : months) {
            String date = month + ".31";
            LambdaQueryWrapper<Member> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.le(date != null, Member::getRegTime,date);
            Long count = memberMapper.selectCount(queryWrapper);
            memberCount.add(Math.toIntExact(count));
        }
        return memberCount;
    }

    @Override
    public List<Map<String, Object>> findSetmealCount() {
        return setMealMapper.findSetmealCount();
    }
}
